home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 009a / newtk200.zip / NEWTRACK.CPP < prev    next >
C/C++ Source or Header  |  1993-03-11  |  3KB  |  130 lines

  1. // @(#) %M% V%I% %H%
  2. //
  3. // File name
  4. // -------------
  5. // NEWTRACK.CPP
  6. //
  7. // (c) Cavendish Software Ltd 1991, 1992
  8. //
  9. // Document References
  10. // -------------------
  11. // This is a general purpose module, and as such has no document references
  12. //
  13. // Document References
  14. // -------------------
  15. // This is a general purpose module, and as such has no document references
  16. //
  17. // File Description
  18. // ----------------
  19. // Newtrack v2.0/Windows.  This part of NewTrack must be included in
  20. //    each .EXE or .DLL.
  21. //
  22.  
  23. // PREPROCESSOR SYMBOL DEFINITIONS -- Complete Exclusion
  24. #if (!defined(USE_NEWTRACK)) || (USE_NEWTRACK)
  25.     
  26.  
  27. // HEADER FILES
  28. #include <alloc.h>
  29. #include <dos.h>
  30. #include <windows.h>
  31. #include <newtrack.hpp>
  32.  
  33.  
  34. // PREPROCESSOR SYMBOL DEFINITIONS
  35. #define USE_WINDOWSALLOC        0
  36.  
  37.  
  38. // GLOBAL VARIABLES
  39. int _far __newtrack = 1;
  40.  
  41.  
  42. // FUNCTIONS
  43.  
  44. // Deletes the pointer, with validation if active
  45. void operator delete(void far *p)
  46. {
  47.     // If it's a valid pointer or there is no module checking, free it up
  48.     if (!__newtrack || (p = NT_Delete((NT_PTR) p)) != 0)
  49.     {
  50. #if USE_WINDOWSALLOC
  51.         HGLOBAL    hglb = *(((HGLOBAL *)p) - 1);
  52.         if (GlobalUnlock(hglb) == 0)
  53.             GlobalFree(hglb);
  54. #else
  55.         free(p);
  56. #endif
  57.     }
  58. }
  59.  
  60. // Allocates a pointer, tracking the pointer if active
  61. void far *operator new(unsigned size)
  62. {
  63.     unsigned int    cseg;
  64.     unsigned int    coff;
  65.  
  66.     NT_GETCALLER(2, cseg, coff);
  67.     return NT_AllocMem(size, MK_FP(cseg, coff));
  68. }
  69.  
  70. // Allocates a pointer, tracking the pointer if active
  71. void far *operator new(unsigned long size)
  72. {
  73.     unsigned int    cseg;
  74.     unsigned int    coff;
  75.  
  76.     NT_GETCALLER(4, cseg, coff);
  77.     return NT_AllocMem(size, MK_FP(cseg, coff));
  78. }
  79.  
  80. // Does the actual allocation & tracking.
  81. void far *NT_AllocMem(unsigned long _size, void far *caller)
  82. {
  83. #if USE_WINDOWSALLOC
  84.     NT_SIZE size;
  85.  
  86.     // Work out the size to allocate
  87.     if (!__newtrack)
  88.         size = _size;
  89.     else
  90.         size = NT_NewSize(_size);
  91.  
  92.     // Allocate it
  93.     HGLOBAL        hglb = GlobalAlloc(GPTR | GMEM_NODISCARD | GMEM_SHARE,
  94.                         size + sizeof(HGLOBAL));
  95.     void far    *p = 0;
  96.  
  97.     // Lock the memory (if we got any)
  98.     if (hglb)
  99.         p = GlobalLock(hglb);
  100.  
  101.     // If we couldn't lock it, free any that we did allocate
  102.     if (!p && hglb)
  103.         GlobalFree(hglb);
  104.  
  105.     // Save the Handle in the first few bytes, and move the pointer past it
  106.     if (p)
  107.     {
  108.         *((HGLOBAL *)p) = hglb;
  109.         p = (void far *)(((HGLOBAL *)p) + 1);
  110.     }
  111.  
  112.     // If NewTrack is not wanted, return now we have the actual memory
  113.     if (!__newtrack)
  114.         return p;
  115.  
  116.     // Return a NewTrack manipulated pointer to the data
  117.     return (void far *)NT_New(p, size, caller);
  118. #else
  119.     if (!__newtrack)
  120.         return farmalloc(_size);
  121.  
  122.     NT_SIZE        size = NT_NewSize(_size);
  123.  
  124.     return (void far *)NT_New(farmalloc(size), size, caller);
  125. #endif
  126. };
  127.  
  128.  
  129. #endif // !def(USE_NEWTRACK) || (USE_NEWTRACK != 0)
  130.